import type { EnergyDataItem } from './type'

export const identity = Symbol('选中type')
export const handleIdentity = Symbol('处理函数')
export const statusIdentity
  = Symbol('控制同比、环比内容显示')

export function rand(n: number = 3000, m: number = 2000) {
  return +(Math.random() * n + m).toFixed(2)
}
export function randWithSummary(r: number, c: number) {
  return Array.from({ length: r }).map(() => {
    const value = Array.from({ length: c }).map(() =>
      rand(),
    )
    return [
      ...value,
      +value.reduce((a, b) => a + b, 0).toFixed(2),
    ]
  })
}

/** 综合能耗标识 */
export const ENERGY_COMPREHENSIVE = 'ENERGY_COMPREHENSIVE'

interface C {
  time: string
  list: {
    pointName: string
    pointId: string
    val: string
  }[]
}
export function convertResToEnergyData(array: any[], key: 'shiftName' | 'teamName' = 'shiftName'): EnergyDataItem[] {
  return array.map((v) => {
    return {
      name: v[key],
      data: (v.dataList || []).map((vv: any) => ({
        key: vv.key,
        value: vv.val,
      })),
    }
  })
}
/** 补全能源数据 */
export function completeEnergyData(res: any[], key: 'shiftName' | 'teamName'): EnergyDataItem[] {
  const array = convertResToEnergyData(res, key)
  const dataArray = array.reduce((acc, cur) => {
    acc = acc.concat(cur.data)
    return acc
  }, [] as EnergyDataItem['data'])

  const keys: EnergyDataItem['data'][number]['key'][] = []
  dataArray.forEach((v) => {
    if (!keys.includes(v.key)) {
      keys.push(v.key)
    }
  })
  return array.map((v) => {
    const data = v.data.reduce((acc, cur) => {
      acc[cur.key] = cur.value
      return acc
    }, {} as Record<EnergyDataItem['data'][number]['key'], any>)
    return {
      ...v,
      data: keys.map((key) => {
        return {
          key,
          value: data[key] || '--',
        }
      }),
    }
  })
}
